local table = require "table"

local bst = require "bfmoon.bst"

local tests = {}

function tests.iteration()
	local values = {8,6,7,4,12,13,10}
	local tree = bst:new_multi(values)
	table.sort(values)
	local results = {}
	for n, v in tree:iter() do
		table.insert(results, v)
	end
	for i, v in ipairs(results) do
		assert(v == values[i])
	end
end

function tests.reverse_iteration()
	local values = {8,6,7,4,12,13,10}
	local tree = bst:new_multi(values)
	table.sort(values, function(v1, v2) return v1>v2 end)
	local results = {}
	for n, v in tree:rev_iter() do
		table.insert(results, v)
	end
	for i, v in ipairs(results) do
		assert(v == values[i])
	end
end

function tests.search()
	local tree = bst:new_multi{8,6,7,4,12,13,10}
	for i, v in ipairs{8,6,7,4,12,13,10} do
		assert(tree:search(v))
	end
	for i, v in ipairs{5,3,11,14,9} do
		assert(not tree:search(v))
	end
end

function tests.delete()
	local tree = bst:new_multi{8,6,7,4,12,13,10}
	tree:delete_multi{4,6,12}
	for i, v in ipairs{8,7,13,10} do
		assert(tree:search(v))
	end
	for i, v in ipairs{4,6,12} do
		assert(not tree:search(v))
	end
end

function tests.all()
	for name, test in pairs(tests) do
		if name ~= "all" then
			print("running test "..name)
			test()
		end
	end
end

return tests
